package bots;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class BotUtil {

	/**
	 * Mali test primer za uporedjivanje 2 grada
	 * */
	public static void main(String[] args) {
		String grad1, grad2;
		double dice;

		grad1 = "Cuprija";
		grad2 = "Cuprija AS";

		dice = dice(bigram(grad1), bigram(grad2));

		System.out.println("\n\tGrad1 = " + grad1 + "\n\tGrad2 = " + grad2
				+ "\n\tDice = " + dice);
		System.out.println("=== " + istiGradovi(grad1, grad2));

		grad1 = "Krusevac";
		grad2 = "Kragujevac";

		dice = dice(bigram(grad1), bigram(grad2));

		System.out.println("\n\tGrad1 = " + grad1 + "\n\tGrad2 = " + grad2
				+ "\n\tDice = " + dice);
		System.out.println("=== " + istiGradovi(grad1, grad2));

		grad1 = "BACKA TOPOLA";
		grad2 = "BACKA PALANKA";

		dice = dice(bigram(grad1), bigram(grad2));

		System.out.println("\n\tGrad1 = " + grad1 + "\n\tGrad2 = " + grad2
				+ "\n\tDice = " + dice);
		System.out.println("=== " + istiGradovi(grad1, grad2));

		grad1 = "BEOGRAD";
		grad2 = "NOVI BEOGRAD";

		dice = dice(bigram(grad1), bigram(grad2));

		System.out.println("\n\tGrad1 = " + grad1 + "\n\tGrad2 = " + grad2
				+ "\n\tDice = " + dice);
		System.out.println("=== " + istiGradovi(grad1, grad2));

		grad1 = "KRUSEVAC";
		grad2 = "KRUŠEVAC";

		System.out.println(grad1.length() + " " + grad2.length());

		dice = dice(bigram(grad1), bigram(grad2));

		System.out.println("\n\tGrad1 = " + grad1 + "\n\tGrad2 = " + grad2
				+ "\n\tDice = " + dice);
		System.out.println("=== " + istiGradovi(grad1, grad2));
	}

	public static String getHtmlFromPostMethod(String url, String[] names,
			String[] values) {
		try {
			// posto imamo POST metodu, ne mozemo samo proslediti URL, nego
			// konekciju u koju smo postavili parametre

			DefaultHttpClient httpclient = new DefaultHttpClient();
			HttpPost httpost = new HttpPost(url);

			List<NameValuePair> nvps = new ArrayList<NameValuePair>();

			for (int i = 0; i < names.length; i++) {
				nvps.add(new BasicNameValuePair(names[i], values[i]));
			}

			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

			HttpResponse response = httpclient.execute(httpost);
			HttpEntity ent = response.getEntity();

			OutputStream output = new ByteArrayOutputStream();
			ent.writeTo(output);

			httpclient.getConnectionManager().shutdown();

			return output.toString().trim();

		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static boolean istiGradovi(String grad1, String grad2) {
		double dice = dice(bigram(grad1), bigram(grad2));

		// ovo mozemo korigovati, ako procenat pogadjanja bude mali
		if (dice >= 0.75)
			return true;
		else
			return false;
	}

	private static List<char[]> bigram(String input) {
		ArrayList<char[]> bigram = new ArrayList<char[]>();
		for (int i = 0; i < input.length() - 1; i++) {
			char[] chars = new char[2];
			chars[0] = input.charAt(i);
			chars[1] = input.charAt(i + 1);
			bigram.add(chars);
		}
		return bigram;
	}

	private static double dice(List<char[]> bigram1, List<char[]> bigram2) {
		List<char[]> copy = new ArrayList<char[]>(bigram2);
		int matches = 0;
		for (int i = bigram1.size(); --i >= 0;) {
			char[] bigram = bigram1.get(i);
			for (int j = copy.size(); --j >= 0;) {
				char[] toMatch = copy.get(j);
				if (bigram[0] == toMatch[0] && bigram[1] == toMatch[1]) {
					copy.remove(j);
					matches += 2;
					break;
				}
			}
		}
		return (double) matches / (bigram1.size() + bigram2.size());
	}
	

}
